// pages/correctGather/correctGather.js
import util from '../../utils/util.js';
var app = getApp();
import weRequest from '../../utils/weRequest.js';
import commonAPI from '../../api/api'
const FN = require('../../utils/public');
const voice = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vsId:"",
    page:1,
    priTmplIdArr:[],
    total:0,
    userType:"",
    voiceResult:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getSubscribeMsg();// 订阅消息
    that.setData({
      vsId:options.vsId,
      vsTitle:options.vsTitle
    })
  },

  
  // 获取订阅消息
  getSubscribeMsg: function () {
    var that = this;
    commonAPI.getSubscribeMsg({ pageUrl: util.getCurrentPageUrl() }).then(res => {
      if (res.code == '200' && res.data.length > 0) {
        var arr = res.data[0].priTmplIdArr.split(",")
        that.setData({
          priTmplIdArr: arr
        })
      }
    })
  },
 
  initData: function () {
    var that = this;
    commonAPI.getVrListByVSid({
      pageNum: that.data.page,
      pageSize: 15,
      voiceSystemId: that.data.vsId
    }).then(res => {
      if (res.code == '200') {
        var tmp = that.data.voiceResult;
        var resArr = new Array();
        res.data.list.map(function (item, i, arr) {
          item.voiceResultInfo = JSON.parse(item.voiceResultInfo);
          if (isNaN(item.voiceResultAccuracy)) {
            item.voiceResultAccuracy = 0;
          }
          resArr.push(item);
        })
        wx.hideNavigationBarLoading(); // 完成停止加载图标
        wx.stopPullDownRefresh(); // 停止下拉
        if (resArr.length == 0) {
          FN.Toast("没有更多数据了")
        } else {
          tmp.push.apply(tmp, resArr); //上拉加载
          that.setData({
            voiceResult: tmp,
            total: res.data.total
          })
        }
      }
    })
  },

    // 学员回复提醒
    subscribeMessage: function () {
      var that = this;
      wx.requestSubscribeMessage({
        tmplIds: that.data.priTmplIdArr,
        // ['jom-rRTvCvgYhW8wGboFjLFPPe0lARWn7g6ZmgjfWGA','mAQgNAtlUHK0blK_9XmIHOQWGOHLfenDc1VAM3p1uQ0'], // 收到预约申请通知,奖励领取通知
        success(res) {
          console.log('授权成功！');
        }
      })
    },

  // 去纠音 
  correct:function(e){
    var vrid = e.currentTarget.dataset.vrid, that = this;
    that.subscribeMessage();
    var baid = '';
    wx.navigateTo({
      url: '../admin4TeacherPage/teaReplyStucorrect/teaReplyStucorrect?vrId=' + vrid+'&freeCorrectMode=true'
    })
  },

  // 返回前面的页面
  backPage:function(){
    wx.navigateBack({
      delta: 1
    })
  },

  // 播放声音
  playVoice: function (e) {
    var i = e.currentTarget.dataset.index;
    var obj = this.data.voiceResult[i].voiceResultInfo;
    voice.stop();
    voice.src = obj.tempFilePath;
    voice.play();
    voice.onPlay(function () {
      wx.showLoading({
        title: '播放中',
      })
    });

    voice.onStop(function () {
      wx.hideLoading();
    });

    // 音频结束回调
    voice.onEnded(function () {
      wx.hideLoading();
    });
  },

   // 查看老师纠音
   repeatPage:function(e){
    //const ue = wx.getStorageSync("wxInfoDiff");
    // var i = e.currentTarget.dataset.vrid;
    // var voiceResultId = this.data.voiceResult[i].voiceResultId;
    var vrid = e.currentTarget.dataset.vrid;
    //var voiceResultId = this.data.voiceResult[i].voiceResultId;
    wx.navigateTo({
      url: '../repeat/repeat?vrid=' +vrid+'&mode=2'
    })
   },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      voiceResult:[]
    })
    this.initData();
    setTimeout(() => {
      this.setData({
        userType:app.globalData.userInfo.userType
      })
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var i = this.data.page;
    i++;
    this.setData({
      page:i
    })
    this.initData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})